let App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled: false,
    nav_select: false, // 快捷导航
    region: '',
    detail: {},

    error: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 获取当前地址信息
    this.getAddressDetail(options.address_id);
  },

  /**
   * 获取当前地址信息
   */
  getAddressDetail: function(address_id) {
    let _this = this;
    App._get('address/'+address_id, {
    }, function(result) {
      let data = _this.initData(result.data);
      _this.setData(data);
    });
  },

initData(datas){
    let _this = this;
     let data={
    disabled: false,
    nav_select: false, // 快捷导航
    region: '',
    detail: {name:"",detail:""},

    error: '',
  }
  let arr = datas.userAddressList[0].region.merger_name.split(',');
  data.region = arr[1]+','+arr[2]+','+arr[3];
  data.detail.detail= datas.userAddressList[0].userAddress.detail;
  data.detail.name = datas.userAddressList[0].userAddress.name;
  data.detail.phone = datas.userAddressList[0].userAddress.phone;
  data.detail.address_id = datas.userAddressList[0].userAddress.id;
  return data;
},

  /**
   * 表单提交
   */
  saveData: function(e) {
    let _this = this,
      values = e.detail.value
    values.region = this.data.region;
    let exp=values.region;
    if(exp[0]=="北京市") 
      exp[0]="北京";
    else if(exp[0]=="上海市") exp[0]="上海";
    else if(exp[0]=="天津市") exp[0]="天津";
    else if(exp[0]=="重庆市"){
        exp[0]="重庆";
        exp[1]="重庆市";
    }
    values.region=exp[0]+','+exp[1]+','+exp[2]; 
    // 记录formId
    // App.saveFormId(e.detail.formId);

    // 表单验证
    if (!_this.validation(values)) {
      App.showError(_this.data.error);
      return false;
    }

    // 按钮禁用
    _this.setData({
      disabled: true
    });



     let datas = {
      "name":values.name,
      "phone":values.phone,
      "merger_name":"中国,"+values.region,
      "detail":values.detail
    };
    console.log("手势是",datas.merger_name);



    // 提交到后端
    values.address_id = _this.data.detail.address_id;
    //let dat = {searchMap:datas,id:_this.data.detail.address_id}
    App._post_form('address/modify/'+_this.data.detail.address_id, 
    datas, 
    function(result) {
      console.log("sh数据sss",result.message);
      App.showSuccess(result.message, function() {
        console.log("sh数据",result.msg);
        wx.navigateBack();
      });
    }, false, function() {
      // 解除禁用
      _this.setData({
        disabled: false
      });
    });
  },

  /**
   * 表单验证
   */
  validation: function(values) {
    if (values.name === '') {
      this.data.error = '收件人不能为空';
      return false;
    }
    if (values.phone.length < 1) {
      this.data.error = '手机号不能为空';
      return false;
    }
    if (values.phone.length !== 11) {
      this.data.error = '手机号长度有误';
      return false;
    }
    let reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!reg.test(values.phone)) {
      this.data.error = '手机号不符合要求';
      return false;
    }
    if (!this.data.region) {
      this.data.error = '省市区不能空';
      return false;
    }
    if (values.detail === '') {
      this.data.error = '详细地址不能为空';
      return false;
    }
    return true;
  },

  /**
   * 修改地区
   */
  bindRegionChange: function(e) {
    this.setData({
      region: e.detail.value
    })
  },

})